package com.project.springboot.controller;

import com.gitee.yongzhuzl.commonutil.util.result.ResponseResult;
import com.project.springboot.service.file.FileService;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.File;
import java.util.Arrays;
import java.util.List;

/**
 * description: 文件相关API
 * @author 朱正磊
 * @date 2023-12-18 10:11:29
 */
@RestController
@RequestMapping("/file")
public class FileController {

    @Resource
    private FileService fileService;

    /**
     * description: 下载文件到服务器
     * @author 朱正磊
     * @date 2023-12-18 10:14:24
     * @return 无业务返回值
     */
    @GetMapping("/downloadFileToServer")
    public ResponseResult<Void> downloadFileToServer() {
        String fileUrl= "https://t7.baidu.com/it/u=963301259,1982396977&fm=193&f=GIF";
        String filePath = "F:\\zzl\\workfile\\图片\\其他\\";
        String extension = ".gif";
        fileService.downloadFileToServer(fileUrl, filePath, extension);
        return ResponseResult.buildSuccess();
    }

    /**
     * description: 下载文件到客户端
     * @author 朱正磊
     * @date 2023-12-18 10:14:24
     * @return 返回HTTP响应对象
     */
    @GetMapping("/downloadFileToClient")
    public ResponseEntity<FileSystemResource> downloadFileToClient() {
        String filePath = "F:\\zzl\\workfile\\图片\\卡通\\katong.jpeg";
        return fileService.downloadFileToClient(new File(filePath));
    }

    /**
     * description: 下载zip压缩文件到客户端
     * @author 朱正磊
     * @date 2023-12-18 10:14:24
     * @return 返回HTTP响应对象
     */
    @GetMapping("/downloadZipToClient")
    public ResponseEntity<FileSystemResource> downloadZipToClient() {
        String zipPathDir = "F:\\zzl\\workfile\\图片\\卡通\\";
        String zipFileName = "test.zip";
        List<String> fileList = Arrays.asList("F:\\zzl\\workfile\\图片\\卡通\\katong.jpeg",
                "F:\\zzl\\workfile\\图片\\其他\\fengjing.gif", "F:\\zzl\\workfile\\图片\\药品\\黄芩.jpeg");
        return fileService.downloadZipToClient(zipPathDir, zipFileName, fileList);
    }

}
